package com.example.gokchinesefoodmapmcdev.controller;

import com.example.gokchinesefoodmapmcdev.entity.DTO.SubmitReturnDTO;
import com.example.gokchinesefoodmapmcdev.entity.sys.SysUser;
import com.example.gokchinesefoodmapmcdev.service.ClientService;
import com.example.gokchinesefoodmapmcdev.service.OrderService;
import com.example.gokchinesefoodmapmcdev.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * 客户功能控制端
 */
@RestController
@RequestMapping("/rest/app")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @Autowired
    private OrderService orderService;

    /**
     * 1. 用户注册
     * @param sysUser   用户注册信息
     * @return          以JSON字符串形式的返回结果
     */
    @PostMapping("/register")
    public ResultData register(@RequestBody SysUser sysUser){
        // 填充时间
        Date date = new Date();
        sysUser.setGmtCreate(date);
        sysUser.setGmtModified(date);

        ResultData resultData = clientService.chickSysUserInfo(sysUser);
        if((Integer) resultData.get("code") == ResultData.REGISTER_FAIL){
            return resultData;
        }

        Integer result = clientService.sysUserRegister(sysUser);
        if(result <= 0){
            return ResultData.fail(ResultData.REGISTER_FAIL, "注册失败，请稍后再试");
        }
        return ResultData.ok(ResultData.REGISTER_SUCCESS, "注册成功");
    }


    /*
     * 根据分页信息，查询普通订单列表
     * */
    @RequestMapping("/order/list")
    public Map<String,Object> orderList(Integer pageSize,Integer pageNumber,Integer orderStatus){
        ResultData resultData = orderService.selectOrderList(pageSize, pageNumber, orderStatus);
        return resultData;
    }


    /*
     * 查询普通订单详情
     * */
    @RequestMapping("/order/info")
    public Map<String,Object> orderInfo(String orderNo){


        ResultData resultData = orderService.selectOrderInfoByOrderNo(orderNo);
        return resultData;
    }

    /*
    * 取消订单
    * */
    @RequestMapping("/order/cancelOrder")
    public Map<String,Object> cancelOrder(String orderNo){
        ResultData resultData = orderService.cancelOrder(orderNo);
        return resultData;
    }

    /*
     * 确认收货
     * */
    @RequestMapping("/order/confirmReceipt")
    public Map<String,Object> confirmReceipt(String orderNo){

        ResultData resultData = orderService.updateOrderStatusByOrderNo(orderNo);
        return resultData;

    }

    /*
     * 删除订单
     * */
    @RequestMapping("/order/remove")
    public Map<String,Object> removeOrder(String orderNo){

        ResultData resultData = orderService.deleteOrderByOrderNo(orderNo);
        return resultData;

    }

    /*
     * 退款分页列表查询
     * */
    @RequestMapping("/orderReturn/list")
    public Map<String,Object> orderReturnList(Integer pageSize,Integer pageNumber){
        ResultData resultData = orderService.orderReturnList(pageSize, pageNumber);
        return resultData;

    }

    /*
     * 发起退款申请
     * */
    @RequestMapping("/order/submitReturn")
    public Map<String,Object> submitReturn(@RequestBody SubmitReturnDTO submitReturnDTO){

        //获取订单信息参数
        String returnReason = submitReturnDTO.getReturnReason();
        String orderSubNo = submitReturnDTO.getOrderSubNo();
        ResultData resultData = orderService.insertOrderReturnInfo(returnReason, orderSubNo);

        if("200".equals(resultData.get("code"))){
            return resultData;
        }
        submitReturnDTO.getListOrderReturnImageEntity().forEach(r->{
            String returnOrderImageUrl = r.getReturnOrderImageUrl();
            orderService.insertOrderReturnImage(orderSubNo, returnOrderImageUrl);
        });

        return resultData;
    }

    /*
     * 查看退款详情,根据前端传入的returnOrderId
     * */
    @RequestMapping("/orderReturn/info")
    public Map<String,Object> orderReturnInfo(Integer returnOrderId){

        ResultData resultData = orderService.selectOrderReturnInfoByReturnOrderId(returnOrderId);
        return resultData;


    }



    /**
     * 普通订单支付宝支付-参数获取
     */
   /* @RequestMapping("/order/alipay")
    public ResultData payOrder(@RequestBody Map<String, String> map){

        String pubNo = map.get("pubNo");



        return null;
    }*/



}
